package com.beit.mybatis.demo.common.component;

import jakarta.annotation.PostConstruct;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class MessageHelper {
    private static MessageSource innerMessageSource;

    private final MessageSource messageSource;

    private MessageHelper(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @PostConstruct
    void init() {
        MessageHelper.innerMessageSource = this.messageSource;
    }

    @Nullable
    public static String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage) {
        return innerMessageSource.getMessage(code, args, defaultMessage, locale());
    }

    public static String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException {
        return innerMessageSource.getMessage(code, args, locale());
    }

    static Locale locale() {
        return Locale.CHINA;
    }
}
